/*
 * @Author: zxc
 * @Date: 2020-08-02 10:44:05
 * @LastEditTime: 2020-08-13 19:53:42
 * @LastEditors: zxc
 * @Description: 队列
 */ 
export class Queue {
  constructor() {
    this.count = 0; //队列大小
    this.lowestCount = 0; //追踪第一个元素
    this.items = {};
  }
  //向队列尾部添加元素
  enqueue(element) {
    this.items[this.count] = element;
    this.count++;
  }
  //从队列移除元素
  dequeue() {
    if (this.isEmpty()) {
      throw new Error('队列为空')
    }
    const result = this.items[this.lowestCount];
    delete this.items[this.lowestCount];
    this.lowestCount++;
    return result;
  }
  //队列头元素查看
  peek(){
    if(this.isEmpty()){
      throw new Error('队列为空')
    }
    return this.items[this.lowestCount];
  }
  //判空
  isEmpty() {
    return this.size() === 0;
  }
  //清空队列
  clear(){
    this.items = {}
    this.count = 0;
    this.lowestCount = 0;
  }
  //队列大小
  size(){
    return this.count - this.lowestCount;
  }
  //toString
  toString(){
    if(this.isEmpty()) {
      throw new Error('队列为空')
    }
    let objString = `${this.items[this.lowestCount]}`
    for(let i = this.lowestCount+1;i<this.count; i++) {
      objString = `${objString},${this.items[i]}`
    }
    return objString;
  }
}

let queue = new Queue();

